<?php

namespace App\Controller\LazyCommon;

use App\Base\BaseConfig;
use App\Base\BaseController4User;
use App\Model\LazyCommon\MessageModel;
use App\Model\LazyCommon\OrdersModel;
use App\Model\LazyCommon\ShopGoodsModel;
use App\Entity\User;
use App\Model\LazyCommon\UserLoginLogModel;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserGroupsModel;
use App\Model\LazyCommon\UserInviteModel;
use App\Model\LazyCommon\UserLikesShopModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use App\Base\BaseController;
use App\Entity\User  as Entity;
use App\Setting\LazyCommon\UserSetting;

/**
 * @route("/app")
 */
class UserAppHomeController extends BaseController4User
{

    /**
     * @route("/shop/home",name="AppShopHome")
     */
    public function AppShopHome()
    {
        $uid = $this->needToBeUser();
        $UserModel = new UserModel($this->getDoctrine());
        $user = $UserModel->getUserById($uid);
        $data = $user->toArray();

        //获取推荐码
        $UserInviteModel = new UserInviteModel($this->getDoctrine());
        $userInvite = $UserInviteModel->getUser($uid);
        $data["code"] = $userInvite->getCode();

        $url = BaseConfig::__BASE_API__."/web/reg/page?code=". $userInvite->getCode();
        $data["qrCode"] = BaseConfig::__BASE_API__."/Qr?code=". urlencode($url);

        //获取商品订单粉丝数量
        //商品
        $ShopGoodsModel = new ShopGoodsModel($this->getDoctrine());
        $numGoods = $ShopGoodsModel->getNum4Shop($uid);
        $data["numGoods"] = $numGoods;

        //订单
        $OrdersModel = new OrdersModel($this->getDoctrine());
        $numOrders = $OrdersModel->getNum4Shop($uid);
        $data["numOrders"] = $numOrders;


        //粉丝
        $UserLikesShopModel = new UserLikesShopModel($this->getDoctrine());
        $numFans = $UserLikesShopModel->getNum4Shop($uid);
        $data["numFans"] = $numFans;



        return $this->json_out(1,"数据获取成功",$data);
    }

    /**
     * @route("/user/home",name="AppUserHome")
     */
    public function AppUserHome()
    {
        $uid = $this->needToBeUser();
        $UserModel = new UserModel($this->getDoctrine());
        $user = $UserModel->getUserById($uid);
        $data = $user->toArray();

        //获取推荐码
        $UserInviteModel = new UserInviteModel($this->getDoctrine());
        $userInvite = $UserInviteModel->getUser($uid);
        $data["code"] = $userInvite->getCode();

        $url = BaseConfig::__BASE_API__."/web/reg/page?code=". $userInvite->getCode();
        $data["qrCode"] = BaseConfig::__BASE_API__."/Qr?code=". urlencode($url);

        //获取商品订单粉丝数量
        //订单
        $OrdersModel = new OrdersModel($this->getDoctrine());
        $numOrders = $OrdersModel->getNum4User($uid);
        $data["numOrders"] = $numOrders;

        //店铺关注
        $UserLikesShopModel = new UserLikesShopModel($this->getDoctrine());
        $numFans = $UserLikesShopModel->getNum4User($uid);
        $data["numShops"] = $numFans;

        return $this->json_out(1,"数据获取成功",$data);
    }

    /**
     * @route("/user/index",name="AppUserIndex")
     */
    public function AppUserIndex()
    {
        $uid = $this->needToBeUser();

        $OrdersModel = new OrdersModel($this->getDoctrine());
        $data = $OrdersModel->getRecentShopAndGood($uid);



        $MessageModel = new MessageModel($this->getDoctrine());

        if ($this->isShop()) {

            $list = $MessageModel->getList4Shop($uid,0,3);
        } else {

            $list = $MessageModel->getList4User($uid,0,3);
        }
        if (!$list) {
            $list = "";
        }
        foreach ($list as &$row) {
            $row["content"] = strip_tags($row["content"]);
        }


        $data["message"] = $list;

        return $this->json_out(1,"获取成功",$data);
    }

}
